/*----------------------------------------------------------------------*/
/* FAT file system test terminal for FatFs R0.07a  (C)ChaN, 2008        */
/* modified by Martin Thomas 4/2009 for the STM32 example               */
/*----------------------------------------------------------------------*/

#include <string.h>
#include <CoOS.h>
#include <stdio.h>
#include <diskio.h>
#include <ff.h>
#include <rtc.h>
#include <integer.h>
#include <Config.h>
#include <ParseString.h>
#include <Fat_Func.h>

extern USART_TypeDef* USARTx;
//DWORD acc_size; /* Work register for fs command */
//WORD acc_files, acc_dirs;
FILINFO Finfo;
#if _USE_LFN
char Lfname[512];
#endif

//char linebuf[120]; /* Console input buffer */

FATFS Fatfs[_DRIVES];
FIL File1;
//FIL File2;
DIR Dir;
BYTE Buff[4 * 1024] __attribute__ ((aligned (4)));
/* Working buffer */

//volatile UINT Timer; /* Performance timer (1kHz increment) */

//static
//void put_rc(FRESULT rc) {
//	const char *p;
//	static const char
//			str[] =
//					"OK\0" "NOT_READY\0" "NO_FILE\0" "FR_NO_PATH\0" "INVALID_NAME\0" "INVALID_DRIVE\0"
//						"DENIED\0" "EXIST\0" "RW_ERROR\0" "WRITE_PROTECTED\0" "NOT_ENABLED\0"
//						"NO_FILESYSTEM\0" "INVALID_OBJECT\0" "MKFS_ABORTED\0";
//	FRESULT i;
//
//	for (p = str, i = 0; i != rc && *p; i++) {
//		while (*p++)
//			;
//	}
//	printf("rc=%u FR_%s\n", (UINT) rc, p);
//}

/***************************************************************************************************
 * Function Name : OpenFile
 * Input		 : file name
 * Output		 : 0: exist file, 1: new file
 * Description 	 : Open file on SD card
 ****************************************************************************************************/
int CreateFile(char* filename, BYTE mode) {
	int status = 0;
	f_mount(0, &Fatfs[0]);
	if (f_open(&File1, filename, 1) != 0) {
		f_open(&File1, filename, mode);
		status = 1;
	}
	f_close(&File1);
	return status;
}
/***************************************************************************************************
 * Function Name : OpenFile
 * Input		 : file name
 * Output		 : 0: exist file, 1: new file
 * Description 	 : Open file on SD card
 ****************************************************************************************************/
void CreateNewFile(char* filename, BYTE mode) {
	f_mount(0, &Fatfs[0]);
	f_open(&File1, filename, mode);
	f_close(&File1);
}
/***************************************************************************************************
 * Function Name : WriterFile
 * Input		 : file name
 * Output		 : 0: exist file, 1: new file
 * Description 	 : Open file on SD card
 ****************************************************************************************************/
void WriteFile(char* filename, const char* data, DWORD ofs) {
	UINT s2 = sizeof(Buff);
	f_open(&File1, filename, 2);
	f_lseek(&File1, ofs);
	memcpy(Buff, (BYTE*) data, strlen(data) + 1);
	f_write(&File1, Buff, strlen(data), &s2);
	f_close(&File1);

}

/***************************************************************************************************
 * Function Name : GetFileSize
 * Input		 : file name
 * Output		 : size of file
 * Description 	 : Get size of file
 ****************************************************************************************************/
void AppendFile(char* filename, char* data) {
	unsigned int size = 250;
	DWORD size_total = 0;
	USARTx = USARTTerm;
	size_t str_size = 0;
	uint32_t rc;
	rc = f_open(&File1, filename, 1);
	if (rc == 3) {
		printf("\r\nError");
	}
	do {
		f_lseek(&File1, size_total);

		f_read(&File1, Buff, 3 * 1024, &size);

		str_size = strlen((char*) Buff);
		size_total = size_total + size;
		if ((size == 0) && (str_size == 0)) {
			break;
		}
	} while (size == str_size);
	f_close(&File1);
	WriteFile(filename, data, size_total);
}

/**********************************************************************************
 *
 *
 *
 *
 */

unsigned int ReadFileAll(char* filename, char* str, unsigned int size,
		DWORD ofs) {
	//	char tBuff[size];
	//	memset(Buff, 0, sizeof(Buff));
	//	f_open(&File1, filename, 1);
	//	f_lseek(&File1, (DWORD) ofs);
	//	f_read(&File1, Buff, size, &size);
	//	strcpy(str, (char*) Buff);
	//	f_close(&File1);
	//	return size;
	char tBuff[size + 1];
	ClearString(tBuff);
	f_open(&File1, filename, FA_READ);
	f_lseek(&File1, (DWORD) ofs);
	f_read(&File1, tBuff, size, &size);
	strcpy(str, tBuff);
	f_close(&File1);
	return size;
}

/********************************************************************
 * open dir
 *
 * put list of file (info) onto "FILINFO *List"
 * return: number of items under dir
 * 		   -1 open fail
 * 		   -2 read fail
 ********************************************************************/
int ListDir(const char *PATH, FILINFO *List) {
	unsigned int pos;
	FRESULT fres = f_chdir(PATH);
	if (f_opendir(&Dir, PATH) != 0) {
		return -1;
	}
	pos = 0;
	for (;;) {
		if (f_readdir(&Dir, &Finfo) != 0) {
			return -2;
		}
		if (strcmp(Finfo.fname, "") == 0) {
			List[pos] = Finfo;
			break;
		}
		List[pos++] = Finfo;
	}
	return pos - 1;
}

/*
 *
 */
unsigned int CheckIfFile(char* filename) {
	unsigned int res = 0;
	DIR dir_tmp;
	res = f_opendir(&dir_tmp, filename);
	if (res != 0) {
		return 1;
	} else {
		return 0;
	}
}

/*
 *
 */
void PathGoBack(char *PATH) {
	int pos = 0;
	for (pos = strlen(PATH) - 1; pos >= 0; pos--) {
		if (PATH[pos] == '/') {
			char path_tmp[128];
			substring(0, pos, PATH, path_tmp);
			strcpy(PATH, path_tmp);
			break;
		}
	}
}

/*******************************************************************************
 * mount sd card
 *
 * return 1 if OK
 * 		  0 if ERROR
 *******************************************************************************/
int MountingSDcard() {
	f_mount(0, &Fatfs[0]);
	FRESULT res = f_open(&File1, "0:NULL.txt", 10);
	if ((res == FR_NO_FILE) || (res == FR_OK)) {
		f_close(&File1);
		return 1;
	} else {
		return 0;
	}
}

